package aggregate.aggregator;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ListIterator;
import java.util.Vector;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import aggregate.aggregator.remote.ContentItem;

public class Feed {
	private int id;
	private String url;
	private String lastNewsTitle;
	private SAXHandler handler;
	
	/****************************************/
	
	public Feed(String url)
	{
		this.url = url;
		this.handler = new SAXHandler();
		this.lastNewsTitle = "";
	}
	
	public String getLastNewsTitle() {
		return lastNewsTitle;
	}
	
	public void setLastNewsTitle(String title) {
		this.lastNewsTitle = title;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	/****************************************/
	
	public void setLastNewsTitle()
	{
		ListIterator<ContentItem> iterator = this.handler.getUpdates().listIterator();
		if(iterator.hasNext())
			this.lastNewsTitle = ((ContentItem) iterator.next()).getTitle();
		
		
	}
	
	/****************************************/
	
	public String getURL()
	{
		return url;
	}
	
	/****************************************/
	
	public boolean equals(String url)
	{
		return this.url.equals(url);
		
	}
	
	/****************************************/
	
	public Vector<ContentItem> getUpdates()
	{
		return this.handler.getUpdates();
	}
	
	/****************************************/
	
	public int verifyUpdates()
	{
		
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser;
		
				
		try
		{
			URL urlObject = new URL(this.url); 
			HttpURLConnection conex = (HttpURLConnection)urlObject.openConnection();
			BufferedReader flux_in = new BufferedReader (new InputStreamReader(conex.getInputStream())); 
			String linie; 
			PrintStream flux_out = new PrintStream (new FileOutputStream("rss.xml"));
	        while ((linie = flux_in.readLine()) != null) 
	        {
	        	flux_out.println(linie);
	        }
	        flux_in.close();
	        flux_out.close(); 

			
		}catch(Exception e)
		{
			System.out.println("Exception: "+e.getMessage());
		}
		
		
		try
		{
			this.handler.setLastNews(this.lastNewsTitle);
			parser = factory.newSAXParser();
			parser.parse("rss.xml", this.handler);
						
			
		}catch(SAXException se)
		{
			System.out.println("SAXException:\n" + se.getMessage());
		}
		catch(IOException ioe)
		{
			System.out.println("IOException:\n" + ioe.getMessage());
		}
		catch(ParserConfigurationException pce)
		{
			System.out.println("ParserConfigurationException:\n" + pce.getMessage());
		}
		
		return this.handler.numberOfUpdates();

	}
	
	/****************************************************/
	
}
